home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / print.idb / usr / sbin / mkcentpr.z / mkcentpr
Encoding:
Text File  |  1997-07-30  |  11.9 KB  |  462 lines

  1. #!/bin/sh
  2. #Tag 0x00000700
  3. #**************************************************************************
  4. #*
  5. #*           Copyright (c) 1993 Silicon Graphics, Inc.
  6. #*            All Rights Reserved
  7. #*
  8. #*       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  9. #*
  10. #* The copyright notice above does not evidence any actual of intended
  11. #* publication of such source code, and is an unpublished work by Silicon
  12. #* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  13. #* the property of Silicon Graphics, Inc. Any use, duplication or
  14. #* disclosure not specifically authorized by Silicon Graphics is strictly
  15. #* prohibited.
  16. #*
  17. #* RESTRICTED RIGHTS LEGEND:
  18. #*
  19. #* Use, duplication or disclosure by the Government is subject to
  20. #* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  21. #* Technical Data and Computer Software clause at DFARS 52.227-7013,
  22. #* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  23. #* Supplement. Unpublished - rights reserved under the Copyright Laws of
  24. #* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  25. #* Shoreline Blvd., Mountain View, CA 94039-7311
  26. #**************************************************************************
  27. #*
  28. #* File: mkcentpr
  29. #*
  30. #* $Revision: 1.32 $
  31. #*
  32. #* Description: Shell script for adding a Centronics based printer to
  33. #*    the System V spooling system.
  34. #*
  35. #*    Usage: mkcentpr
  36. #*
  37. #*    Selection of all installation parameters such as printer name
  38. #*    will be done interactively.
  39. #*
  40. #* Updated for print 1.7 (major changes for print 1.7).  This script no
  41. #* longer parses the output of "lputil list".  It now uses
  42. #* /usr/lib/print/modelinfo and /usr/lib/print/modinfofltr to build
  43. #* the menus that the user sees.  This was done because with PPD files
  44. #* (introduced in Impressario 2.2) it is possible to have a large number
  45. #* of supported printers.  The older code for this routine could not handle
  46. #* a large number of printers (it would overflow string variables).
  47. #*
  48. #**************************************************************************
  49.  
  50.  
  51. # Well known directories and programs
  52.  
  53. LPUTIL_DIR=/usr/lib
  54. LPUTIL=$LPUTIL_DIR/lputil
  55. AWK_DIR=/usr/bin
  56. AWK=$AWK_DIR/nawk
  57. OUT_FILT="/usr/bin/pg -s -p more... -e"
  58. SPOOL_DIR=/var/spool/lp
  59. LPSTAT=/usr/bin/lpstat
  60. MODELINFO=/usr/lib/print/modelinfo
  61. MODELINFOFILTER="/usr/lib/print/modinfofltr -l centronics"
  62.  
  63. # Global variables
  64.  
  65. PNAME=""
  66. DEF_CENT_DEV=""
  67. CENT_DEV=""
  68. PMODEL_LIST=""
  69. PTYPE_LIST=""
  70. POPT_LIST=""
  71. NUM_MODELS=0
  72. NUM_VENDORSS=0
  73. TYPE_ID=""
  74. VENDOR_ID=""
  75. TYPE_NAME=""
  76. MODEL_NAME=""
  77. OPTION_STR=""
  78.  
  79.  
  80. #
  81. # Checks that we are the super-user and verifies
  82. # that the System V spooling system is present
  83. #
  84. CheckPermSpooler()
  85. {
  86.     cid=`/usr/bin/id | $AWK '{print substr($1,index($1,"("))}'`;
  87.     if [ "$cid" != "(root)" ]; then
  88.         echo "You must be logged in as root to use this program."
  89.         exit 1;
  90.     fi
  91.     if [ ! -r $SPOOL_DIR ]; then
  92.         echo "The System V spooling system is not installed on this system."
  93.         exit 1
  94.     fi
  95. }
  96.  
  97.  
  98. #
  99. # Determines the default parallel port based on the
  100. # system IP number.
  101. #
  102. FindDefPort()
  103. {
  104.     # Assume we have /dev/plp 
  105.     DEF_CENT_DEV=/dev/plp
  106.  
  107. }
  108.  
  109.  
  110. #
  111. # Extracts the model file name from a string similar to:
  112. # "laserwriter_model%CENTRONICS%Apple LaserWriter II%FACEUP=0".
  113. # Would return "laserwriter_model".
  114. #
  115. GetModelFileName()
  116. {
  117.     MODEL_NAME=`echo $* | $AWK  -F\%  '{
  118.             printf("%s\n",$1)
  119.         }'`
  120. }
  121.  
  122.  
  123. #
  124. # Extracts the printer type from a string similar to:
  125. # "laserwriter_model%CENTRONICS%Apple LaserWriter II%FACEUP=0"
  126. # Would return "Apple LaserWriter II".
  127. #
  128. GetPrinterType()
  129. {
  130.     TYPE_NAME=`echo $* | $AWK -F\% '{
  131.             printf("%s\n",$3)
  132.         }'`
  133. }
  134.  
  135.  
  136. #
  137. # Extracts the printer model file options from a string similar to:
  138. # "laserwriter_model%CENTRONICS%Apple LaserWriter II%FACEUP=0"
  139. # Would return "FACEUP=0".
  140. #
  141. GetPrinterOptions()
  142. {
  143.     OPTION_STR=`echo $* | $AWK -F\% '{
  144.             printf("%s\n",$4)
  145.         }'`
  146. }
  147.  
  148. #
  149. # Test for pure decimal positive integer input
  150. #
  151. # Expects $1 to be the value to check. Returns 0 if
  152. # OK, 1 if invalid positive integer or if more than one
  153. # argument specified
  154. #
  155. VerifyInt()
  156. {
  157.     if [ $# -eq 0 ]; then
  158.     return 0
  159.     fi
  160.     if [ $# -gt 1 ]; then
  161.     return 1
  162.     fi
  163.     echo $1 | egrep -s '^[0-9]*$'
  164.     return $?
  165. }
  166.  
  167.  
  168. #
  169. # Test for a valid printer name (i.e. 14 chars max, A-Za-z0-9_
  170. # only and no spaces)
  171. #
  172. VerifyPname()
  173. {
  174.     if [ $# -gt 1 ]; then
  175.     return 1
  176.     fi
  177.     echo $1 | egrep -s '^[A-Za-z0-9_]*$'
  178.     if [ $? -eq 1 ]; then
  179.     return 1
  180.     fi
  181.     if [ `echo $1 | $AWK '{ print length($1) }'` -gt 14 ]; then
  182.     return 1
  183.     else
  184.     return 0
  185.     fi
  186. }
  187.  
  188.  
  189. #########################################################################
  190. #
  191. # Main program
  192. #
  193.  
  194. #
  195. # Ensure that we are root and that the main System V
  196. # spooling system directory is present
  197. #
  198. CheckPermSpooler
  199.  
  200. #
  201. # Display an intro message
  202. #
  203. echo "Centronics Printer Installation Tool\n"
  204.  
  205. #
  206. # Query user for a name for this printer
  207. #
  208. while [ "$PNAME" = "" ]; do
  209.     echo "Enter new printer name (14 chars max.): \c"
  210.     read PNAME
  211.     VerifyPname $PNAME
  212.     if [ $? -eq 1 ]; then
  213.     echo "Invalid response: Name must be 14 chars max. (A-Za-z0-9_ only)"
  214.     PNAME=""
  215.     fi
  216.     if [ "$PNAME" = "" ]; then
  217.     continue
  218.     fi
  219.     /bin/ls $SPOOL_DIR/member/$PNAME 2> /dev/null 1> /dev/null
  220.     if [ $? -eq 0 ]; then
  221.     echo ""
  222.         echo "WARNING: Printer $PNAME already exists."
  223.         echo '\nChoose a new name (y/n)[y]? \c'
  224.         read yn
  225.         if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
  226.         PNAME=""
  227.         fi
  228.     fi
  229. done
  230. echo " "
  231.  
  232. #
  233. # Determine the default parallel port
  234. #
  235. FindDefPort
  236.  
  237. #
  238. # Query the user for the device port
  239. #
  240. while [ "$CENT_DEV" = "" ]; do
  241.     echo "Enter centronics device port [$DEF_CENT_DEV]: \c"
  242.     read CENT_DEV
  243.     if [ "$CENT_DEV" = "" ]; then
  244.     CENT_DEV=$DEF_CENT_DEV
  245.     fi
  246. done
  247. echo " "
  248.  
  249. #
  250. # Sanity check the device port by seeing if we can ls it
  251. #
  252. /bin/ls $CENT_DEV 2> /dev/null 1> /dev/null
  253. if [ $? -ne 0 ]; then
  254.     echo "WARNING: Device file $CENT_DEV does not exist."
  255.     echo '\nContinue installation (y/n)[n]? \c'
  256.     read yn
  257.     if [ \( "$yn" != "y" \) -a \( "$yn" != "Y" \) ]; then
  258.     exit 1
  259.     fi
  260.     echo " "
  261. fi
  262.  
  263. #
  264. # Check to see if another printer is using this port
  265. #
  266. in_use=`cd $SPOOL_DIR/member; egrep -l $CENT_DEV * 2> /dev/null`
  267. if [ "$in_use" != "" ]; then
  268.     echo "WARNING: Device file $CENT_DEV is currently used by printer(s):"
  269.     for p in $in_use; do
  270.     echo "\t\t$p"
  271.     done
  272.     echo '\nContinue installation (y/n)[n]? \c'
  273.     read yn
  274.     if [ \( "$yn" != "y" \) -a \( "$yn" != "Y" \) ]; then
  275.     exit 1
  276.     fi
  277.     echo " "
  278. fi
  279.  
  280. #
  281. # Use modelinfo and modinfofltr to build menus of supported printer
  282. # vendors and printer models.
  283. #
  284. echo "Determining printer vendor list..."
  285. echo " "
  286.  
  287. # The outer loop is because we allow the user to press return onthe
  288. # secoond menu to jump back to the first menu.
  289.  
  290. GOT_VENDOR_ID=""
  291. while [ "$GOT_VENDOR_ID" = "" ]; do
  292.  
  293.    # Get the user choice of vendor.  User sees something like:
  294.    # 1. GENERIC
  295.    # 2. HP
  296.    # 3. TEK
  297.  
  298.    VENDOR_ID=""
  299.    while [ "$VENDOR_ID" = "" ]; do
  300.       MODELS_LIST=`$MODELINFO | $MODELINFOFILTER`
  301.       NUM_VENDORS=$?  # modinfoftr returns number of menu items
  302.       echo "$MODELS_LIST" | $OUT_FILT
  303.       if [ $NUM_VENDORS -eq  -1 ]; then
  304.           echo "ERROR. $MODELINFOFILTER returns -1.  -1 indicates"
  305.           echo "program called with invalid arguments."
  306.       fi
  307.       if [ $NUM_VENDORS -eq 0 ]; then
  308.          echo "ERROR: No parallel printers appear to be supported on this system."         
  309.          echo "       Check that print software has been installed."
  310.          exit 1
  311.       fi
  312.       echo " "
  313.       echo "Select printer vendor to use: \c"
  314.       read VENDOR_ID
  315.       VerifyInt $VENDOR_ID
  316.       if [ $? -eq 1 ]; then
  317.            echo "Invalid response: must be number between 1 and $NUM_VENDORS"
  318.            VENDOR_ID=""
  319.       fi
  320.       if [ "$VENDOR_ID" = "" ]; then
  321.            continue
  322.       fi
  323.       if [ \( $VENDOR_ID -eq 0 \) -o \( $VENDOR_ID -gt $NUM_VENDORS \) ]; then
  324.            echo "Invalid response: must be between 1 and $NUM_VENDORS"
  325.            VENDOR_ID=""
  326.       fi
  327.    done
  328.  
  329.    GOT_VENDOR_ID="1"  # Force exit from main loop
  330.  
  331.    # Now list the printer drivers supported for the selected vendor:
  332.    # Get user choice for model. User sees something like:
  333.  
  334.    # 1. APPLE LASERWRITER II
  335.    # 2. APPLE LASERWRITER IIF
  336.    # 3. APPLE LASERWRITER IIG
  337.    # 4. APPLE LASERWRITER IINT
  338.    # 5. APPLE LASERWRITER IINTX
  339.    # 6. APPLE LASERWRITER PLUS
  340.  
  341.    TYPE_ID=""
  342.    while [ "$TYPE_ID" = "" ]; do
  343.  
  344.       $MODELINFO | $MODELINFOFILTER -v$VENDOR_ID | egrep -i "\(POSTSCRIPT\)" 2> /dev/null 1> /dev/null
  345.       PPD_FOUND=$?
  346.  
  347.       $MODELINFO | $MODELINFOFILTER -v$VENDOR_ID | egrep "IMPRESSARIO LICENSE REQUIRED" 2> /dev/null 1> /dev/null
  348.       LICENSE_NEEDED=$?
  349.  
  350.       echo " "
  351.       MODELS_LIST=`$MODELINFO | $MODELINFOFILTER -v$VENDOR_ID`
  352.       NUM_MODELS=$?  # modinfoftr returns number of menu items
  353.       echo "$MODELS_LIST" | $OUT_FILT
  354.       if [ $NUM_MODELS -eq  -1 ]; then
  355.           echo "ERROR. $MODELINFOFILTER returns -1.  -1 indicates"
  356.           echo "program called with invalid arguments."
  357.       fi
  358.  
  359.       if [ $NUM_MODELS -eq  0 ]; then
  360.          echo "ERROR: No parallel printers appear to be supported for this choice."
  361.          echo "       Check that print software has been installed."
  362.          if [ "$NUM_VENDORS" = "1" ]; then
  363.             exit 1
  364.          fi
  365.          echo ""
  366.          echo "Press Enter to return to last menu.\c"
  367.          read TYPE_ID
  368.          TYPE_ID=1         # Bogus value to get out of this loop
  369.          GOT_VENDOR_ID=""  # Re-start main loop to go back to last menu
  370.          continue
  371.       fi
  372.  
  373.       if [ "$PPD_FOUND" = "0" ]; then
  374.         echo ""
  375.         echo "NOTE: Printer model names followed by \"(POSTSCRIPT)\" will only work"
  376.         echo "with printers that have built-in PostScript support.  Some of"
  377.         echo "these printers do not come with built-in PostScript and it must"
  378.         echo "be purchased from the printer vendor as a separate option."
  379.       fi
  380.  
  381.       if [ "$LICENSE_NEEDED" = "0" ]; then
  382.         echo ""
  383.         echo "Printer model names followed by \"(IMPRESSARIO LICENSE REQUIRED)\" will" 
  384.         echo "need an optional Impressario license.  See the Impressario release notes"
  385.         echo "for details or the file /usr/lib/print/data/psrip_expired_msg"
  386.       fi
  387.  
  388.       echo ""
  389.       echo "Select printer model to use (press Enter for last menu): \c"
  390.       read TYPE_ID
  391.       if [ "$TYPE_ID" = "" ]; then
  392.          TYPE_ID=1         # Bogus value to get out of this loop
  393.          GOT_VENDOR_ID=""  # Re-start main loop to go back to last menu
  394.          echo ""
  395.          continue
  396.       fi
  397.       VerifyInt $TYPE_ID
  398.       if [ $? -eq 1 ]; then
  399.            echo "Invalid response: must be number between 1 and $NUM_MODELS"
  400.            TYPE_ID=""
  401.       fi
  402.       if [ \( $TYPE_ID -eq 0 \) -o \( $TYPE_ID -gt $NUM_MODELS \) ]; then
  403.            echo "Invalid response: must be between 1 and $NUM_MODELS"
  404.            TYPE_ID=""
  405.       fi
  406.    done
  407.  
  408. done
  409.  
  410. #
  411. # Inform the user of what comes next
  412. #
  413. echo " "
  414. echo "Installing parallel printer $PNAME..."
  415. echo " "
  416.  
  417. #
  418. # Get the model info string for the selected printer and pull out
  419. # the parts we need.
  420. #
  421.  
  422. modelstring=`$MODELINFO | $MODELINFOFILTER -v$VENDOR_ID -c$TYPE_ID`
  423. if [ $? -eq  -1 ]; then
  424.     echo "ERROR. $MODELINFOFILTER returns -1.  -1 indicates"
  425.     echo "program called with invalid arguments."
  426. fi
  427.  
  428. GetModelFileName $modelstring
  429. GetPrinterType $modelstring
  430. GetPrinterOptions $modelstring
  431.  
  432. #
  433. # Install the printer. We use awk to execute the lputil command
  434. # because we want the OPTION_STR strings handled properly (i.e.
  435. # with the proper quoting).
  436. #
  437. echo $LPUTIL add $CENT_DEV $MODEL_NAME \
  438.     $PNAME NAME=\'\"$TYPE_NAME\"\' $OPTION_STR | $AWK '{
  439.         rv = system($0)
  440.         exit rv
  441.     }'
  442. if [ $? -ne 0 ]; then
  443.     echo "ERROR: Installation of new printer failed."
  444.     echo "       $LPUTIL command failed."
  445.     exit 1
  446. fi
  447.  
  448. #
  449. # Inform the user of what has happened
  450. #
  451. echo " "
  452. echo "Centronics Printer $PNAME has been installed"
  453. echo " "
  454. echo "Here is your printing environment:"
  455. echo " "
  456. $LPSTAT -t | $OUT_FILT
  457.  
  458. #
  459. # Return a successful exit code
  460. #
  461. exit 0
  462.